package com.glela.biz.impl;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.glela.biz.RedPacketShareHistBiz;
import com.glela.cache.redis.RedisKeys;
import com.glela.cache.redis.RedisUtil;
import com.glela.common.util.JsonUtil;
import com.glela.common.util.StringUtil;
import com.glela.domain.vo.RedPacketShareHistResponseVo;
import com.glela.domain.vo.RedPacketShareHistVo;
import com.glela.domain.vo.ShareRedPacketInfoVo;
import com.glela.packet.exception.RedPacketBizException;
import com.glela.packet.service.RedPacketShareHistService;
import com.glela.platform.model.BaseRestrictions;
import com.glela.platform.service.BaseRestrictionsService;
import com.glela.platform.util.ParameterUtils;

@Service("redPacketShareHistBiz")
public class RedPacketShareHistBizImpl implements RedPacketShareHistBiz {

	@Autowired
	@Qualifier(value = "redPacketShareHistService")
	private RedPacketShareHistService redPacketShareHistService;

	@Autowired
	private RedisUtil redisUtil;

	@Autowired
	@Qualifier(value = "baseRestrictionsService")
	private BaseRestrictionsService baseRestrictionsService;

	private static final String RED_PACKET_SHARE_INFO = "red_packet_share_info";

	@Override
	public RedPacketShareHistResponseVo showRedPacketShareLists(Long userId, int startIndex, int pageSize) throws IOException {
		RedPacketShareHistResponseVo redPacketShareresponse = new RedPacketShareHistResponseVo();
		BigDecimal ioo = new BigDecimal(10000);
		List<RedPacketShareHistVo> subList = new ArrayList<>();
		List<RedPacketShareHistVo> list = new ArrayList<>();
		ShareRedPacketInfoVo parseObj = null;
		String key = RedisKeys.RED_PACKET_SHARE + userId ;
		String keyII = RedisKeys.RED_PACKET_INFO;
		if (!StringUtils.isEmpty(redisUtil.get(key))) {
			RedPacketShareHistResponseVo obj = JSON.parseObject(redisUtil.get(key), RedPacketShareHistResponseVo.class);
			
			//redis 中的价格已经去掉两个0 补齐
			if(null != obj.getTotalAmount()){
				obj.setTotalAmount(obj.getTotalAmount().multiply(ioo).setScale(0));
			}
			List<RedPacketShareHistVo> redPacketShareHistList = obj.getRedPacketShareHistList();
			for (RedPacketShareHistVo perRedPacketVo : redPacketShareHistList) {
				if(null != perRedPacketVo.getAmount()){
					perRedPacketVo.setAmount(perRedPacketVo.getAmount().multiply(ioo).setScale(0));
				}
			}
			
			
			if(startIndex <=obj.getRedPacketShareHistList().size() && startIndex+pageSize >= obj.getRedPacketShareHistList().size()){
			   subList = obj.getRedPacketShareHistList().subList(startIndex, obj.getRedPacketShareHistList().size());
			}else if(startIndex > obj.getRedPacketShareHistList().size()){
				//return null;
			}else{
				 subList = obj.getRedPacketShareHistList().subList(startIndex, startIndex+pageSize);
			}
			obj.setRedPacketShareHistList(subList);
			return obj;
		}
		// 分享红包总额
		BigDecimal total = redPacketShareHistService.querySumAmount(userId);
		// 分享红包总条数
		int times = redPacketShareHistService.queryShareTimes(userId);
		redPacketShareresponse.setTotal(times);
		redPacketShareresponse.setTotalAmount(total);
		
		// 分享红包列表
		list = redPacketShareHistService.showRedPacketShareLists(userId);
		redPacketShareresponse.setRedPacketShareHistList(list);
		if (null != list && list.size() != 0) {
			for (RedPacketShareHistVo redPacketShare : list) {
				if ("0".equals(redPacketShare.getStatus())) { // 状态是未领取 8限时分享 9 永久分享
					if ("0".equals(redPacketShare.getShareType())) { // 0：限时分享；1：永久分享
						redPacketShare.setStatus("8");
					}
					if ("1".equals(redPacketShare.getShareType())) {
						redPacketShare.setStatus("9");
					}
					redPacketShare.setDesc("还没有人领取哦");
				}
				if ("3".equals(redPacketShare.getStatus())) { // 状态是已退回
																// 状态：0未领取；1：已领取；2：已使用；3：已退回
					if ("0".equals(redPacketShare.getReceiveUserId())) { // 领取人id为空
																					// 无人领取
						redPacketShare.setDesc("分享后24小时无人领取已退回");
						redPacketShare.setNickName(null);
						redPacketShare.setPicture(null);
					}
					if (!"0".equals(redPacketShare.getReceiveUserId())) { // 领取人id不为空
																					// 显示领取人退回的
						redPacketShare.setNickName(redPacketShare.getBackName());
						redPacketShare.setPicture(redPacketShare.getBackPicture());
					}
				}
				//已使用   已领取 显示领取人头像 昵称
				if("1".equals(redPacketShare.getStatus()) || "2".equals(redPacketShare.getStatus())){
					if (!"0".equals(redPacketShare.getReceiveUserId())) { // 领取人id不为空
						// 显示领取人退回的
						redPacketShare.setNickName(redPacketShare.getBackName());
						redPacketShare.setPicture(redPacketShare.getBackPicture());
					}
				}
				
				
				if (!StringUtils.isEmpty(redisUtil.get(keyII))) {
					String res = redisUtil.get(keyII);
					parseObj = JSON.parseObject(res, ShareRedPacketInfoVo.class);
				} else {
					BaseRestrictions baseRestrictions = baseRestrictionsService.getBaseRestrictionsForCache(RED_PACKET_SHARE_INFO);
					if (baseRestrictions != null && !baseRestrictions.getName().isEmpty()) {
						parseObj = JSON.parseObject(baseRestrictions.getName(), ShareRedPacketInfoVo.class);
						redisUtil.set(keyII, JsonUtil.toJson(parseObj));
					}
				}
				if (parseObj != null) {  
					redPacketShare.setActiveDesc(parseObj.getActiveDesc());
					redPacketShare.setStoreUserName(parseObj.getStoreUserName());
					//redPacketShare.setSharePicture(parseObj.getImagePic() + redPacketShare.getSharePicture());
					
					redPacketShare.setSharePicture(parseObj.getImagePic());

					redPacketShare.setShareUrl(parseObj.getShareUrl() + redPacketShare.getShareUrl()+"&talentId="+userId);
				}
			}
			redPacketShareresponse.setRedPacketShareHistList(list);
		}
		if (redPacketShareresponse.getRedPacketShareHistList().size() != 0 && 0 != redPacketShareresponse.getTotal()) {
			redisUtil.set(key, JsonUtil.toJson(redPacketShareresponse), 300000);
		}
		if(startIndex <=list.size() && startIndex+pageSize >= list.size()){
			subList = list.subList(startIndex, list.size());
		}else if(startIndex > list.size()){
			
		}else{
			 subList = list.subList(startIndex, startIndex+pageSize);
		}
		redPacketShareresponse.setRedPacketShareHistList(subList);
		return redPacketShareresponse;
	}

}
